<?php
/**
 * 收藏接口
 * 收藏，取消收藏，是否收藏
 * 验证用户身份token
 * @author Edwin
 */

namespace V1\Controller;

use Common\Common\Tools;
use Common\Controller\ApiController;
use Think\Exception;

class FavouriteController extends ApiController
{
    /**
     * rest路由
     */
    public function rest()
    {
        switch ($this->_method) {
            case 'get': // post请求处理代码
                $this->getFavouriteInfo();
                break;
            case 'post': // post请求处理代码
                $this->addFavaourite();
                break;
            case 'delete':
                $this->removeFavourite();
                break;
        }
    }

    /**
     * 查询收藏
     * @param uid [用户id]
     * @param askid [问题id]
     * @return array
     */
    private function getFavouriteInfo()
    {
        $info = I('get.');
        $info['uid'] = !empty($info['uid']) ? $info['uid'] : $this->userId;
        $info['ask_id'] = !empty(I('get.ask_id')) ? trim(I('get.ask_id')) : '';
        try {
            $data = D('Favourite')->get_favourite_info($info);
            if ($data > 0) {
                $this->myApiPrint(C('STATUS')['232'], 200, true);
            } else {
                $this->myApiPrint(C('STATUS')['231'], 201, false);
            }
        } catch (Exception $exception) {
            $this->myApiPrint(C('STATUS')['230'], 400, false);
        }
    }

    /**
     * 添加收藏
     */
    private function addFavaourite()
    {
        $info = I('post.');
        $info['uid'] = !empty($info['uid']) ? $info['uid'] : $this->userId;
        $data = D('Favourite')->add_favourite($info);
        if (!$data) {
            $this->myApiPrint(C('STATUS')['215'], 201);
        } else {
            $this->myApiPrint(C('STATUS')['200'], 200);
        }
    }

    /**
     * 取消收藏
     * @param uid [用户id]
     * @param askid [问题id]
     * @return array
     */
    private function removeFavourite()
    {
        $info = I('get.');
        $info['uid'] = !empty($info['uid']) ? $info['uid'] : $this->userId;
        $data = D('Favourite')->remove_favourite($info);
        if ($data == true) {
            $this->myApiPrint(C('STATUS')['200'], 200);
        } else {
            $this->myApiPrint(C('STATUS')['219'], 201);
        }
    }

    /**
     * 获取个人收藏列表
     */
    public function getFavaouriteList()
    {
        $uid = !empty(I('get.uid')) ? I('get.uid') : $this->userId;
        try {
            $data = D('Favourite')->get_simple_favourite($uid);
            if ($data) {
                foreach ($data as $k => $v) {
                    $data[$k]['addtime_date'] = format_date($v['addtime']);
                    $data[$k]['ask_images'] = [];
                    $data[$k]['image_count'] = '';
                    for ($i = 0; $i < 6; $i++) {
                        if ($v['thumb' . $i]) {
                            $data[$k]['ask_images'][] = $v['thumb' . $i];
                            $data[$k]['image_count'] = $i + 1;
                        }
                        unset($data[$k]['thumb' . $i]);
                    }
                    $data[$k]['area'] = Tools::arr2str(D('Area')->get_area_full_name_from_area_id($v['areaid']), ' ');
                }
                $this->myApiPrint(C('STATUS')['200'], 200, $data);
            } else {
                $this->myApiPrint(C('STATUS')['230'], 201, []);
            }
        } catch (Exception $e) {
            $this->myApiPrint(C('STATUS')['101'], 400,[]);
        }
    }
}
